﻿using LiveCharts;
using Microsoft.Win32;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Xls;
using Stylet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NiceComponents.Others.Pages
{
    public class MainViewModel : Screen
    {
        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            ReflashDatas();
        }

        #region Setting
        public bool IsSettingFlyoutOpen { get; set; }
        public void DoSetting()
        {
            IsSettingFlyoutOpen = !IsSettingFlyoutOpen;
        }
        #endregion

        #region LiveCharts

        public string[] XTimeLabels { get; set; } = new string[] { "0" };
        public ChartValues<double> VoltageLineSeries { get; set; }
        public ChartValues<double> CurrentLineSeries { get; set; }

        public void ReflashDatas()
        {
            Random random = new Random();

            VoltageLineSeries = new ChartValues<double>();
            CurrentLineSeries = new ChartValues<double>();

            Task.Run(() =>
            {
                while (true)
                {
                    VoltageLineSeries.Add(random.Next(200, 300));
                    CurrentLineSeries.Add(random.Next(1, 20));

                    int Count = VoltageLineSeries.Count;
                    if (Count > 0)
                    {
                        XTimeLabels = new string[Count];
                        for (int i = 0; i < Count; i++)
                        {
                            XTimeLabels[i] = (i + 1).ToString();
                        }
                    }

                    Thread.Sleep(2000);
                }
            });
        }

        #endregion

        #region FreeSpire

        public void SaveWord()
        {
            SaveFileDialog fileDialog = new SaveFileDialog()
            {
                Filter = "Word File(*.docx)|*.docx",
                FileName = "Report01" + ".docx",
            };

            if (fileDialog.ShowDialog() == true)
            {
                Document document = new Document();
                Section s = document.AddSection();             
                Paragraph para1 = s.AddParagraph();
                para1.AppendText("欢迎使用Spire.Doc");

                document.SaveToFile(fileDialog.FileName, Spire.Doc.FileFormat.Docx);
                Process.Start(fileDialog.FileName);
            }
        }

        public void SaveExcel()
        {
            SaveFileDialog fileDialog = new SaveFileDialog()
            {
                Filter = "Word File(*.xlsx)|*.xlsx",
                FileName = "Report01" + ".xlsx",
            };

            if (fileDialog.ShowDialog() == true)
            {
                Workbook workbook = new Workbook();
                Worksheet sheet = workbook.Worksheets[0];

                sheet.Range[1, 1].Text = "步骤";
                sheet.Range[1, 2].Text = "时间";               

                int row = 2;
                for (int i = 0; i <10; i++)
                {                   
                    sheet.Range[row, 1].Text = i.ToString();
                    sheet.Range[row, 2].Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 
                    row++;
                }

                sheet.Range[row + 2, 1].Text = "报告时间：";
                sheet.Range[row + 2, 2].Text = $"2022-02-02 11:11:11";
              
                workbook.SaveToFile(fileDialog.FileName, ExcelVersion.Version2010);
                Process.Start(fileDialog.FileName);
            }
        }

        public void LoadWord()
        {
            SaveFileDialog fileDialog = new SaveFileDialog()
            {
                Filter = "Word File(*.docx)|*.docx",
                FileName = "Report02" + ".docx",
            };

            if (fileDialog.ShowDialog() == true)
            {
                Document document = new Document();
                document.LoadFromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template", "WordTemplate.docx"));

                document.Replace("<$ReportTitle>", "报表标题", false, true);
                document.Replace("<$CompanyName>", "公司名称", false, true);

                document.SaveToFile(fileDialog.FileName, Spire.Doc.FileFormat.Docx);
                Process.Start(fileDialog.FileName);
            }
        }

        public void SavePdf()
        {
            SaveFileDialog fileDialog = new SaveFileDialog()
            {
                Filter = "Word File(*.pdf)|*.pdf",
                FileName = "Report01" + ".pdf",
            };

            if (fileDialog.ShowDialog() == true)
            {
                //初始化一个PdfDocument实例
                PdfDocument document = new PdfDocument();

                //设置边距
                PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
                PdfMargins margins = new PdfMargins();
                margins.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
                margins.Bottom = margins.Top;
                margins.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
                margins.Right = margins.Left;

                //添加新页
                PdfPageBase page = document.Pages.Add(PdfPageSize.A4, margins);

                //自定义PdfTrueTypeFont、PdfPen实例
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体", 11f), true);
                PdfPen pen = new PdfPen(Color.Black);

                //使用DrawString方法在指定位置写入文本
                string text = "我的第一个C# PDF文档";
                page.Canvas.DrawString(text, font, pen, 100, 50);

                //保存文档
                document.SaveToFile(fileDialog.FileName);
                Process.Start(fileDialog.FileName);
            }
        }

        public void WordToPdf()
        {
            var WordFilePath = @"E:\Report02.docx";
            var PdfFilePath = @"E:\Report02.pdf";

            Document document = new Document();
            document.LoadFromFile(WordFilePath);
            document.SaveToFile(PdfFilePath, Spire.Doc.FileFormat.PDF);
            Process.Start(PdfFilePath);
        }

        #endregion
    }
}
